package day01;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

public class Demo01 {
    public static void main(String[] args) {
        /*
         * 测试,分析 List 性能
         */
        ArrayList<String> list1 = new ArrayList<>();
        LinkedList<String> list2 = new LinkedList<>();
        for (int i=0; i<100000; i++){
            String uuid = UUID.randomUUID().toString();
            list1.add(uuid);
            list2.add(uuid);
        }
        getTest(list1);
        getTest(list2);
        addTest(list1);
        addTest(list2);
    }
    public static void addTest(List<String> list){
        System.out.println(list.getClass());
        //读取list 的头部, 中部, 尾部, 测试其不同位置的读取性能
        int size = list.size();
        long t1 = System.nanoTime();
        list.add(0,"Tom"); //从头插入
        long t2 = System.nanoTime();
        list.add(size/4, "Tom");
        long t3 = System.nanoTime();
        list.add(size/2, "Tom"); //中部插入
        long t4 = System.nanoTime();
        list.add(size, "Tom"); //尾部插入
        long t5 = System.nanoTime();
        System.out.println("头部插入:"+(t2-t1));
        System.out.println("1/4插入:"+(t3-t2));
        System.out.println("中部插入:"+(t4-t3));
        System.out.println("尾部插入:"+(t5-t4));
    }

    public static void getTest(List<String> list){
        System.out.println(list.getClass());
        //读取list 的头部, 中部, 尾部, 测试其不同位置的读取性能
        String str;
        int size = list.size();
        long t1 = System.nanoTime();
        str = list.get(0); //从头读取
        long t2 = System.nanoTime();
        str = list.get(size/4);
        long t3 = System.nanoTime();
        str = list.get(size/2); //中部读取
        long t4 = System.nanoTime();
        str = list.get(size-1); //尾部读取
        long t5 = System.nanoTime();
        System.out.println("头部读取:"+(t2-t1));
        System.out.println("1/4读取:"+(t3-t2));
        System.out.println("中部读取:"+(t4-t3));
        System.out.println("尾部读取:"+(t5-t4));
    }

}
